//
//  WPOrderViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPOrderViewController.h"
#import "WPOrderModel.h"
#import "WPOrderCompeleteCell.h"
#import "WPOrderViewModel.h"
#import "WPRightManagerViewController.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@interface WPOrderViewController ()<UITableViewDataSource, UITableViewDelegate, WPOrderCellDelegate>
@property (nonatomic, strong) UILabel * noMoreLabel;/**< 没有说明 */
@property (nonatomic, strong) UITableView * orderTableView;/**< 订单列表 */
@property (nonatomic, strong) NSMutableArray * orderDataSource;/**< 订单数据源 */
@property (nonatomic, assign) NSInteger currentPage;/**< 当前页码 */
@property (nonatomic, assign) NSInteger totalsPages;/**< 总页码 */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)refreshData;/**< 刷新数据 */


@end

@implementation WPOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.currentPage = 1;
    self.totalsPages = 1;
    self.orderDataSource = [NSMutableArray arrayWithCapacity:0];
}
- (void)initializeUserInterface {
    self.navigationController.navigationBarHidden = NO;
    [self.view addSubview:self.orderTableView];
    __weak typeof(self)weakself = self;
    _orderTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [weakself refreshData];
    }];
    _orderTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [weakself refreshData];
    }];
    [_orderTableView.header beginRefreshing];
}
#pragma mark - 刷新数据
- (void)refreshData {
    if (_currentPage > _totalsPages) {
        [_orderTableView.footer noticeNoMoreData];
        return;
    }
    __weak typeof(self)weakself = self;
    [WPOrderViewModel getSuccessOrdertWithPageNumber:_currentPage andWithSuccessBlock:^(NSArray *resultList, NSInteger pageTotal) {
        if (_currentPage == 1 && _orderDataSource.count) {
            [_orderDataSource removeAllObjects];
        }
        for (WPOrderModel * model in resultList) {
            [_orderDataSource addObject:model];
        }
        if (_orderDataSource.count) {
            if (weakself.noMoreLabel.superview) {
                [weakself.noMoreLabel removeFromSuperview];
            }
        } else {
            if (!weakself.noMoreLabel.superview) {
                [weakself.view addSubview:weakself.noMoreLabel];
            }
        }
        [_orderTableView reloadData];
        [_orderTableView.header endRefreshing];
        [_orderTableView.footer endRefreshing];
        _totalsPages = pageTotal;
    } andWithFailureBlock:^(NSString *fail) {
        [_orderTableView.header endRefreshing];
        [_orderTableView.footer endRefreshing];
        if (!_orderDataSource.count && !weakself.noMoreLabel.superview) {
            [weakself.view addSubview:weakself.noMoreLabel];
        }
    }];
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _orderDataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WPOrderCompeleteCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[WPOrderCompeleteCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.delegate = self;
    [cell setModel:_orderDataSource[indexPath.section]];
    return cell;
}
#pragma mark - UITableViewDelegate
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 170*DHFlexibleHorizontalMutiplier();
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
#pragma mark - private protocol
- (void)deleteOrderWithOrderModel:(WPOrderModel *)model {
    [self initializeAlertControllerWithMessageAndDismiss:@"删除订单"];
}
- (void)checkTicketRightWithOrderModel:(WPOrderModel *)model {
    WPRightManagerViewController * managerVC = [WPRightManagerViewController new];
    managerVC.model = model;
    [self pushToNextController:managerVC withVCTitle:@"订单详情"];
}
#pragma mark -getter
- (UILabel *)noMoreLabel {
    if (!_noMoreLabel) {
        _noMoreLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 30*DHFlexibleHorizontalMutiplier())];
            lab.center = self.view.center;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.text = @"暂无订单信息";
            lab;
        });
    }
    return _noMoreLabel;
}
- (UITableView *)orderTableView {
    if (!_orderTableView) {
        _orderTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, navBar_Fheight, SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.backgroundColor = [UIColor clearColor];
            tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
            tableview;
        });
    }
    return _orderTableView;
}

@end
